package lesson.tcp.test2;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerDemo {
    public static void main(String[] args) throws IOException {
        //1.创建Socket对象
        ServerSocket ss = new ServerSocket(10000);
        //2.等待客户端连接
        System.out.println("服务器等待客户端中。。。");
        Socket accept = ss.accept();
        System.out.println("已获取客户端，开始执行");

        //3.获得输入流对象
        InputStream is = accept.getInputStream();
        byte [] b = new byte[1024];
        int len;
        while ((len = is.read(b)) != -1){
            String s = new String(b,0,len);
            System.out.println(s);
        }
        System.out.println("执行测试");

        //向客户端发送信息
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
        bw.write("服务器已收到");
        bw.newLine();
        bw.flush();

        System.out.println("服务器关闭");
        //服务器关闭
        bw.close();
        is.close();
        accept.close();
        ss.close();
    }
}
